package com.liang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liang.shortlink.project.dao.entity.LinkAccessStatsDO;
import com.liang.shortlink.project.dto.req.ShortLinkGroupStatsReqDTO;
import com.liang.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 短链接基础访问监控持久层
 */
public interface LinkAccessStatsMapper extends BaseMapper<LinkAccessStatsDO> {

    /**
     * 记录基础访问监控数据
     */
    @Insert("""
            INSERT INTO t_link_access_stats (full_short_url, date, pv, uv, uip, hour, weekday, create_time, update_time, del_flag)
            VALUES (#{param.fullShortUrl}, #{param.date}, #{param.pv}, #{param.uv}, #{param.uip}, #{param.hour}, #{param.weekday}, NOW(), NOW(), 0)
            ON DUPLICATE KEY
                UPDATE pv  = pv + #{param.pv}, uv  = uv + #{param.uv}, uip = uip + #{param.uip};
            """)
    void shortLinkStats(@Param("param") LinkAccessStatsDO linkAccessStatsDO);

    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("""
            SELECT tlas.date,
                   SUM(tlas.pv)  AS pv,
                   SUM(tlas.uv)  AS uv,
                   SUM(tlas.uip) AS uip
            FROM t_link tl
                     INNER JOIN
                 t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
            WHERE tlas.full_short_url = #{param.fullShortUrl}
              AND tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = #{param.enableStatus}
              AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tlas.full_short_url, tl.gid, tlas.date;
            """)
    List<LinkAccessStatsDO> listStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内小时基础监控数据
     */
    @Select("""
            SELECT tlas.hour,
                   SUM(tlas.pv) AS pv
            FROM t_link tl
                     INNER JOIN
                 t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
            WHERE tlas.full_short_url = #{param.fullShortUrl}
              AND tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = #{param.enableStatus}
              AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tlas.full_short_url, tl.gid, tlas.hour;
            """)
    List<LinkAccessStatsDO> listHourStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取一周基础监控数据
     */
    @Select("""
            SELECT tlas.weekday, SUM(tlas.pv) AS pv
            FROM t_link tl
                     INNER JOIN t_link_access_stats tlas
                                ON tl.full_short_url = tlas.full_short_url
            WHERE tlas.full_short_url = #{param.fullShortUrl}
              AND tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = #{param.enableStatus}
              AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tlas.full_short_url, tl.gid, tlas.weekday;
            """)
    List<LinkAccessStatsDO> listWeekdayStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内基础监控数据
     */
    @Select("""
            SELECT tlas.date,
                   SUM(tlas.pv)  AS pv,
                   SUM(tlas.uv)  AS uv,
                   SUM(tlas.uip) AS uip
            FROM t_link tl
                     INNER JOIN
                 t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
            WHERE tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = '0'
              AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tl.gid, tlas.date;
            """)
    List<LinkAccessStatsDO> listStatsByGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内小时基础监控数据
     */
    @Select("""
            SELECT tlas.hour,
                   SUM(tlas.pv) AS pv
            FROM t_link tl
                     INNER JOIN
                 t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
            WHERE tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = '0'
              AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tl.gid, tlas.hour;
            """)
    List<LinkAccessStatsDO> listHourStatsGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内一周基础监控数据
     */
    @Select("""
            SELECT tlas.weekday, SUM(tlas.pv) AS pv
            FROM t_link tl
                     INNER JOIN t_link_access_stats tlas
                                ON tl.full_short_url = tlas.full_short_url
            WHERE tl.gid = #{param.gid}
              AND tl.del_flag = '0'
              AND tl.enable_status = '0'
              AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
            GROUP BY tl.gid, tlas.weekday;
            """)
    List<LinkAccessStatsDO> listWeekdayStatsByGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);
}




